package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.EmployeePhysicalRecordMapper;
import com.zhijian.medical.entity.po.EmployeePhysicalRecord;
import com.zhijian.medical.entity.vo.request.EmployeePhysicalRecordSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.EmployeePhysicalRecordSaveVo;
import com.zhijian.medical.entity.vo.request.EmployeePhysicalRecordSearchVo;
import com.zhijian.medical.entity.vo.response.EmployeePhysicalRecordVo;
import com.zhijian.medical.service.EmployeePhysicalRecordService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 职工体检档案业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-14 14:30:08
 */
@Slf4j
@Service
public class EmployeePhysicalRecordServiceImpl implements EmployeePhysicalRecordService {

	@Autowired
    private EmployeePhysicalRecordMapper mapper;

    @Override
    public EmployeePhysicalRecord selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public EmployeePhysicalRecordVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(EmployeePhysicalRecord employeePhysicalRecord) {
        Date now = new Date();
        employeePhysicalRecord.setCreateTime(now);
        employeePhysicalRecord.setUpdateTime(now);
        return mapper.insert(employeePhysicalRecord) > 0 ? employeePhysicalRecord.getId() : 0L;
    }

    @Override
    public Long insertSelective(EmployeePhysicalRecord employeePhysicalRecord) {
        Date now = new Date();
        employeePhysicalRecord.setCreateTime(now);
        employeePhysicalRecord.setUpdateTime(now);
        return mapper.insertSelective(employeePhysicalRecord) > 0 ? employeePhysicalRecord.getId() : 0L;
    }

    @Override
    public boolean update(EmployeePhysicalRecord employeePhysicalRecord) {
        Date now = new Date();
        employeePhysicalRecord.setUpdateTime(now);
        return mapper.updateByPrimaryKey(employeePhysicalRecord) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(EmployeePhysicalRecord record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(EmployeePhysicalRecordSaveOrUpdateVo saveOrUpdateVo) {
        EmployeePhysicalRecord employeePhysicalRecord = new EmployeePhysicalRecord();
        BeanUtils.copyProperties(saveOrUpdateVo, employeePhysicalRecord);
        return this.insert(employeePhysicalRecord) != 0;
    }

    @Override
    public boolean saveList(EmployeePhysicalRecordSaveVo saveOrUpdateVo) {
        this.deleteByCondition(EmployeePhysicalRecordSearchVo.builder().personId(saveOrUpdateVo.getPersonId()).build());
        for (EmployeePhysicalRecordSaveOrUpdateVo vo : saveOrUpdateVo.getRecordList()) {
            vo.setPersonId(saveOrUpdateVo.getPersonId());
            vo.setPersonName(saveOrUpdateVo.getPersonName());
            vo.setPersonGender(saveOrUpdateVo.getPersonGender());
            vo.setPersonGroup(saveOrUpdateVo.getPersonGroup());
            this.save(vo);
        }
        return true;
    }

    @Override
    public boolean modify(EmployeePhysicalRecordSaveOrUpdateVo saveOrUpdateVo) {
        EmployeePhysicalRecord old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        EmployeePhysicalRecord employeePhysicalRecord = new EmployeePhysicalRecord();
        BeanUtils.copyProperties(saveOrUpdateVo, employeePhysicalRecord);
        return this.update(employeePhysicalRecord);
    }

    @Override
    public EmployeePhysicalRecordVo selectOne(EmployeePhysicalRecordSearchVo employeePhysicalRecordSearchVo) {
        return mapper.selectOne(employeePhysicalRecordSearchVo);
     }

    @Override
    public Integer count(EmployeePhysicalRecordSearchVo employeePhysicalRecordSearchVo) {
        return mapper.count(employeePhysicalRecordSearchVo);
    }

    @Override
    public List<EmployeePhysicalRecordVo> selectList(EmployeePhysicalRecordSearchVo employeePhysicalRecordSearchVo) {
        return mapper.selectList(employeePhysicalRecordSearchVo);
    }

    @Override
    public Page<EmployeePhysicalRecordVo> selectPage(EmployeePhysicalRecordSearchVo employeePhysicalRecordSearchVo) {
        AssertUtil.check(Objects.isNull(employeePhysicalRecordSearchVo.getPageNum()) || Objects.isNull(employeePhysicalRecordSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(employeePhysicalRecordSearchVo);
        //分页
        Page<EmployeePhysicalRecordVo> page = new Page<>(true, count, employeePhysicalRecordSearchVo.getPageNum(), employeePhysicalRecordSearchVo.getPageSize());
        employeePhysicalRecordSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<EmployeePhysicalRecordVo> responseVos = this.selectList(employeePhysicalRecordSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<EmployeePhysicalRecord> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(EmployeePhysicalRecordSearchVo employeePhysicalRecordSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(employeePhysicalRecordSearchVo);
    }
    
}